package bb.lanxing.mvp.model;

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.sprint.entity.SprintFirmware;
import bb.lanxing.lib.devices.sprint.model.impl.AbstractSprintServerFirmwareModel;
//import bb.lanxing.network.BiciHttpClient;
import java.io.File;
import java.io.IOException;
import java.util.List;

import bb.lanxing.network.MyHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONException;
import org.json.JSONObject;

public class SprintServerFirmwareModel extends AbstractSprintServerFirmwareModel {
    public static final String SPRINT_VERSION_LIST = "http://file." + MyHttpClient.DOMAIN_BASE_PURE + "/SPRINT/firmware.json";

    @Override
    protected List<SprintFirmware> loadFirmwares() throws IOException {
        Response execute = MyHttpClient.client.newCall(new Request.Builder().url(getVersionListUrl()).get().build()).execute();
        if (execute.isSuccessful()) {
            try {
                return (List) new GsonBuilder().create().fromJson(new JSONObject(execute.body().string()).getJSONArray("FirmwareList").toString(), new TypeToken<List<SprintFirmware>>() {
                }.getType());
            } catch (JSONException unused) {
            }
        }
        return null;
    }

    public String getVersionListUrl() {
        return SPRINT_VERSION_LIST;
    }

    @Override
    public String getFirmwareDir() {
        return new File(DeviceHelper.getWorkDir(9), "fw_sprint").getAbsolutePath();
    }
}
